<template>
  <div class="article">
    <!-- 导航栏 -->
    <van-nav-bar
      fixed
      left-arrow
      @click-left="$router.back()"
      title="文章详情"
    ></van-nav-bar>

    <!--loading-->
    <div class="loading" v-if="loading">
      <van-loading class="inner" type="spinner" color="#1989fa" />
    </div>

    <!-- 文章详情 -->
    <div class="detail" v-else>
      <h3 class="title">{{ article.description }}</h3>
      <div class="author">
        <van-image
          round
          width="1rem"
          height="1rem"
          fit="fill"
          :src="swiperImgUrl + article.pic"
        />
        <div class="text">
          <p class="name">{{ article.author }}</p>
          <p class="time">{{ article.create_date | fromNow }}</p>
        </div>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>

      <!--分割线-->
      <van-divider>END</van-divider>

      <!--按钮-->
      <div class="btns">
        <van-button
          size="small"
          hairline
          plain
          :icon="startype === '-1' ? 'star-o' : 'star'"
          @click="star"
        ></van-button>
        <van-button
          size="small"
          hairline
          @click="zan"
          plain
          :icon="goodtype === '-2' ? 'good-job-o' : 'good-job'"
        ></van-button>
        <van-button size="small" hairline plain icon="share-o"></van-button>
      </div>
    </div>
  </div>
</template>

<script>
import { getArticle, userDataHandle } from '@/api/article'
export default {
  name: 'Article',
  data() {
    return {
      // 控制收藏的数据
      startype: '-1',
      // 控制点赞的数据
      goodtype: '-2',
      article: {},
      loading: true,
      swiperImgUrl: 'http://124.223.14.236:8060/'
    }
  },
  created() {
    this.getArticleinfo()
  },
  // 点赞/取消点赞和收藏/取消收藏
  methods: {
    // 获取文章信息
    async getArticleinfo() {
      const res = await getArticle(this.$route.query.id)
      // console.log(res)
      this.article = res.data.info
      if (res.data.info.isCollect) {
        this.startype = 1
      } else {
        this.startype = '-1'
      }
      if (res.data.info.isLike) {
        this.goodtype = 1
      } else {
        this.goodtype = '-2'
      }
      this.loading = false
    },
    async star() {
      // 判断状态
      if (this.startype === '-1') {
        // 收藏操作
        await userDataHandle('1', this.$route.query.id, 'add')
        this.$toast.success('收藏成功')
        return (this.startype = 1)
      } else {
        // 取消收藏操作
        await userDataHandle('1', this.$route.query.id, 'del')
        this.startype = '-1'
        this.$toast.success('取消成功')
      }
    },
    async zan() {
      // 判断状态
      if (this.goodtype === '-2') {
        // 收藏操作
        await userDataHandle('2', this.$route.query.id, 'add')
        this.$toast.success('点赞成功')
        return (this.goodtype = 1)
      } else {
        // 取消收藏操作
        await userDataHandle('2', this.$route.query.id, 'del')
        this.goodtype = '-2'
        this.$toast.success('取消成功')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.loading {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.article {
  position: absolute;
  left: 0;
  top: 0;
  overflow-y: scroll;
  width: 100%;
  height: 100%;
}
.loading {
  padding-top: 100px;
  text-align: center;
}
.error {
  padding-top: 100px;
  text-align: center;
}
.detail {
  padding: 50px 10px;
  .title {
    font-size: 16px;
  }
  .zan {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    ::v-deep img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
  .btns {
    border-top: 1px solid black;
    background-color: white;
    width: 100%;
    height: 40px;
    position: fixed;
    bottom: 0px;
    margin: 0 auto;
    padding-bottom: 20px;
    .van-button {
      height: 100%;
      margin-right: 50px;
      margin-left: 30px;
    }
    .van-button__icon {
      font-size: 20px;
    }
  }
}
</style>
